import java.util.Scanner;

public class wannianli2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int year,month,monthdays = 0;
		long weekday;
		System.out.print("请输入年份：");
		year = input.nextInt();
		System.out.print("请输入月份：");
		month = input.nextInt();
		System.out.println(year + "年" + month + "月的日历为");
		zhouci();
		weekday = totaldays(year,month);
		
		monthdays =  days( year, month);
		for(int x=0;x<weekday;x++){
			if(weekday == 7){
				System.out.print("");
				break;
			}
			System.out.print("\t");
		}
		for (int n = 1; n <= monthdays; n++) {

			System.out.print(n + "\t");
			weekday = (weekday + 1) % 7;          
			if (weekday == 0)                       // 判断第二天，如果第二天是星期天，换行

				System.out.println();
		
		
			}
	}
	
	
	public static int isLeapYear(int y){                          //判断是否为闰年

		if((y%4==0 && y%100!=0) || y%400==0){
			return 1;
		}
		else
			return 0;
	}
	public static int days(int year,int month){                //判断某年某月多少天

		int y,m;
		y=year;
		m=month;
		switch(m){
		case 1 :	return 31;
		case 3 :	return 31;
		case 5 :	return 31;
		case 7 :	return 31;
		case 8 :	return 31;
		case 10 :	return 31;
		case 12 :	return 31;
		case 2 :    if(isLeapYear(y)==1){
						return 29;
		}
						else{
						return 28;
		}
		case 4:		return 30;
		case 6:		return 30;
		case 9:		return 30;
		case 11:	return 30;
		}
	
		return 0;
}
	public static long totaldays(int year,int month){            
		long days = 0;
		
		for(int i = 1900;i < year; i++){							//判断从1900年1月1日到输入年份月份的总天数

			if(isLeapYear(i)==1){
				days = days+366;
			}
			else{
				days = days +365;
			}
		}
		for(int n =1;n<month;n++){
			days = days + days(year,n);
		}
		
		return days%7+1;  //判断输入月份的第一天是星期几

	}
	
	public static void zhouci(){                                		 // 打印周次

		System.out.println("日\t一\t二\t三\t四\t五\t六");
		}
		
	
}





